package com.bjpowernode.controller;

import com.bjpowernode.entity.Consumption;
import com.bjpowernode.service.ConsumptionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 处理消费类型相关的请求
 *
 * @author 刘逸晖
 */
@RequestMapping(value = "/myWeb/consumption")
public class ConsumptionController {

    /**
     * 处理消费类型相关业务
     */
    private ConsumptionService consumptionService;

    /**
     * 新增消费类型
     *
     * @param consumption 新增数据
     * @return 模型和试图
     */
    @GetMapping(value = "/first_insert.do")
    public ModelAndView firstInsert(Consumption consumption) {
        this.consumptionService.insertConsumption(consumption);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "一级消费类型添加成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 查询所有一级消费类型
     *
     * @return 一级消费类型列表
     */
    @RequestMapping(value = "/first_findAll.do")
    public ModelAndView findFirst() {
        List<Consumption> consumptions = this.consumptionService.selectAllFirstConsumptions();

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("list", consumptions)
                .setViewName("comsumption_first_show");

        return modelAndView;
    }

    /**
     * 根据 id 删除消费类型
     *
     * @param id 消费类型的 id
     * @return 模型和试图
     */
    @RequestMapping(value = "/delete/first.do")
    public ModelAndView deleteFirst(Integer id) {
        this.consumptionService.deleteConsumptionById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "一级消费类型删除成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 根据 id 删除消费类型
     *
     * @param id 消费类型的 id
     * @return
     */
    @RequestMapping(value = "/findById.do")
    public ModelAndView findById(Integer id) {
        Consumption consumption = this.consumptionService.selectConsumptionById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("result", consumption)
                .setViewName("first_Consumption_info");

        return modelAndView;
    }

    /**
     * 根据 id 更新消费类型
     *
     * @param consumption id 极其新数据
     * @return 模型和试图
     */
    @RequestMapping(value = "/update.do")
    public ModelAndView updateConsumption(Consumption consumption) {
        this.consumptionService.updateConsumptionById(consumption);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "消费类型更新成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 查询所有消费类型
     *
     * @return 所有消费类型
     */
    @RequestMapping(value = "/ajax.do")
    @ResponseBody
    public List<Consumption> ajax() {
        return this.consumptionService.selectAllFirstConsumptions();
    }

    public ConsumptionService getConsumptionService() {
        return consumptionService;
    }

    public void setConsumptionService(ConsumptionService consumptionService) {
        this.consumptionService = consumptionService;
    }
}
